﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;


namespace XWS.RandomContactListGenerator
{
    public class ContactListGeneratorWithCustomerId : ContactListGenerator
    {
        
    }
    class Program
    {
        static void Main(string[] args)
        {
            var filename = "output.csv";
            var lines = 1000;
            var showHelp = false;
          
            try
            {
                GetArguments(args, ref filename, ref lines, ref showHelp);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);   
            }
            

            if (showHelp)
            {
                ShowHelp();
                return;
            }


            var generator = new ContactListGenerator(){NumberOfLinesToGenerate = lines, OutputFileName = filename};

            
            generator.GenerateFile();


        }

     
        /// <summary>
        /// Display command line switch options to console
        /// </summary>
        private static void ShowHelp()
        {
            Console.WriteLine("Contact List File Generator Usage");
            Console.WriteLine("/h - help");
            Console.WriteLine("/f <filename> - output file name");
            Console.WriteLine("/r <# of lines> - number of records to generate");
        }

        /// <summary>
        /// Get command line arguments from console
        /// </summary>
        /// <param name="args">command line arguments</param>
        /// <param name="filename"></param>
        /// <param name="lines"></param>
        /// <param name="showHelp"></param>
        private static void GetArguments(string[] args, ref string filename, ref int lines, ref bool showHelp)
        {
            var listOfArgs = args.ToList();

            if (listOfArgs.Contains("/h", StringComparer.OrdinalIgnoreCase))
                showHelp = true;

            if (listOfArgs.Contains("/f", StringComparer.OrdinalIgnoreCase))
            {
                var index = listOfArgs.FindIndex(s => s.Equals("/f", StringComparison.OrdinalIgnoreCase));
                filename = listOfArgs[index + 1];
            }

            if (listOfArgs.Contains("/r", StringComparer.OrdinalIgnoreCase))
            {
                var index = listOfArgs.FindIndex(s => s.Equals("/r", StringComparison.OrdinalIgnoreCase));
                lines = Convert.ToInt32(listOfArgs[index + 1]);
            }
        }
    }
}
